home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / IP Spoofing.sit / IP Spoofing next >
Text File  |  1997-02-21  |  24KB  |  497 lines

  1.  
  2.                          [ IP-spoofing Demystified ]
  3.                       (Trust-Relationship Exploitation)
  4.  
  5.                         by daemon9 / route / infinity
  6.                              for Phrack Magazine
  7.                       June 1996 Guild Productions, kid
  8.  
  9.                        comments to route@infonexus.com
  10.  
  11.         The purpose of this paper is to explain IP-spoofing to the
  12. masses.  It assumes little more than a working knowledge of Unix and
  13. TCP/IP.  Oh, and that yur not a moron...
  14.         IP-spoofing is complex technical attack that is made up of
  15. several components.  (In actuality, IP-spoofing is not the attack, but
  16. a step in the attack.  The attack is actually trust-relationship
  17. exploitation.  However, in this paper,  IP-spoofing will refer to the
  18. whole attack.)  In this paper, I will explain the attack in detail,
  19. including the relevant operating system and networking information.
  20.  
  21.                 [SECTION I.  BACKGROUND INFORMATION]
  22.  
  23.         --[ The Players ]--
  24.  
  25.         A:      Target host
  26.         B:      Trusted host
  27.         X:      Unreachable host
  28.         Z:      Attacking host
  29.         (1)2:   Host 1 masquerading as host 2
  30.  
  31.         --[ The Figures ]--
  32.  
  33.         There are several figures in the paper and they are to be
  34. interpreted as per the following example:
  35.  
  36. ick   host a      control     host b
  37. 1       A       ---SYN--->      B
  38.  
  39. tick:   A tick of time.  There is no distinction made as to *how*
  40. much time passes between ticks, just that time passes.  It's generally
  41. not a great deal.
  42. host a: A machine particpating in a TCP-based conversation.
  43. control: This field shows any relevant control bits set in the TCP
  44. header and the direction the data is flowing
  45. host b: A machine particpating in a TCP-based conversation.
  46.  
  47. In this case, at the first refrenced point in time host a is sending
  48. a TCP segment to host b with the SYN bit on.  Unless stated, we are
  49. generally not concerned with the data portion of the TCP segment.
  50.  
  51.         --[ Trust Relationships ]--
  52.  
  53.         In the Unix world, trust can be given all too easily.  Say you
  54. have an account on machine A, and on machine B.  To facilitate going
  55. betwixt the two with a minimum amount of hassle, you want to setup a
  56. full-duplex trust relationship between them.  In your home directory
  57. at A you create a .rhosts file: `echo "B username" > ~/.rhosts` In
  58. your home directory at B you create a .rhosts file: `echo "A username"
  59. > ~/.rhosts` (Alternately, root can setup similar rules in
  60. /etc/hosts.equiv, the difference being that the rules are hostwide,
  61. rather than just on an individual basis.)  Now, you can use any of the
  62. r* commands without that annoying hassle of password authentication.
  63. These commands will allow address-based authentication, which will
  64. grant or deny access based off of the IP address of the service
  65. requestor.
  66.  
  67.         --[ Rlogin ]--
  68.  
  69.         Rlogin is a simple client-server based protocol that uses TCP
  70. as it's transport.  Rlogin allows a user to login remotely from one
  71. host to another, and, if the target machine trusts the other, rlogin
  72. will allow the convienience of not prompting for a password.  It will
  73. instead have authenticated the client via the source IP address.  So,
  74. from our example above, we can use rlogin to remotely login to A from
  75. B (or vice-versa) and not be prompted for a password.
  76.  
  77.         --[ Internet Protocol ]--
  78.  
  79.         IP is the connectionless, unreliable network protocol in the
  80. TCP/IP suite.  It has two 32-bit header fields to hold address
  81. information.  IP is also the busiest of all the TCP/IP protocols as
  82. almost all TCP/IP traffic is encapsulated in IP datagrams.  IP's job
  83. is to route packets around the network.  It provides no mechanism for
  84. reliability or accountability, for that, it relies on the upper
  85. layers.  IP simply sends out datagrams and hopes they make it intact.
  86. If they don't, IP can try to send an ICMP error message back to the
  87. source, however this packet can get lost as well.  (ICMP is Internet
  88. Control Message Protocol and it is used to relay network conditions
  89. and different errors to IP and the other layers.)  IP has no means to
  90. guarantee delivery.  Since IP is connectionless, it does not maintain
  91. any connection state information.  Each IP datagram is sent out without
  92. regard to the last one or the next one.  This, along with the fact that
  93. it is trivial to modify the IP stack to allow an arbitrarily choosen IP
  94. address in the source (and destination) fields make IP easily subvertable.
  95.  
  96.         --[ Transmission Control Protocol ]--
  97.  
  98.         TCP is the connection-oriented, reliable transport protocol
  99. in the TCP/IP suite.  Connection-oriented simply means that the two
  100. hosts participating in a discussion must first establish a connection
  101. before data may change hands.  Reliability is provided in a number of
  102. ways but the only two we are concerned with are data sequencing and
  103. acknowledgement.  TCP assigns sequence numbers to every segment and
  104. acknowledges any and all data segments recieved from the other end.
  105. (ACK's consume a sequence number, but are not themselves ACK'd.)
  106. This reliability makes TCP harder to fool than IP.
  107.  
  108.         --[ Sequence Numbers, Acknowledgements and other flags ]--
  109.  
  110.         Since TCP is reliable, it must be able to recover from
  111. lost, duplicated, or out-of-order data.  By assigning a sequence
  112. number to every byte transfered, and requiring an acknowledgement from
  113. the other end upon receipt, TCP can guarantee reliable delivery.  The
  114. receiving end uses the sequence numbers to ensure proper ordering of
  115. the data and to eliminate duplicate data bytes.
  116.         TCP sequence numbers can simply be thought of as 32-bit
  117. counters.  They range from 0 to 4,294,967,295.  Every byte of
  118. data exchanged across a TCP connection (along with certain flags)
  119. is sequenced.  The sequence number field in the TCP header will
  120. contain the sequence number of the *first* byte of data in the
  121. TCP segment.  The acknowledgement number field in the TCP header
  122. holds the value of next *expected* sequence number, and also
  123. acknowledges *all* data up through this ACK number minus one.
  124.         TCP uses the concept of window advertisement for flow
  125. control.  It uses a sliding window to tell the other end how much
  126. data it can buffer.  Since the window size is 16-bits a receiving TCP
  127. can advertise up to a maximum of 65535 bytes.  Window advertisement
  128. can be thought of an advertisment from one TCP to the other of how
  129. high acceptable sequence numbers can be.
  130.         Other TCP header flags of note are RST (reset), PSH (push)
  131. and FIN (finish).  If a RST is received, the connection is
  132. immediately torn down.  RSTs are normally sent when one end
  133. receives a segment that just doesn't jive with current connection
  134. (we will encounter an example below).  The PSH flag tells the
  135. reciever to pass all the data is has queued to the aplication, as
  136. soon as possible.  The FIN flag is the way an application begins a
  137. graceful close of a connection (connection termination is a 4-way
  138. process). When one end recieves a FIN, it ACKs it, and does not
  139. expect to receive any more data (sending is still possible, however).
  140.  
  141.         --[ TCP Connection Establishment ]--
  142.  
  143.         In order to exchange data using TCP, hosts must establish a
  144. a connection.  TCP establishes a connection in a 3 step process called
  145. the 3-way handshake.  If machine A is running an rlogin client and
  146. wishes to conect to an rlogin daemon on machine B, the process is as
  147. follows:
  148.  
  149.                 fig(1)
  150.  
  151. 1       A       ---SYN--->      B
  152.  
  153. 2       A    <---SYN/ACK---     B
  154.  
  155. 3       A       ---ACK--->      B
  156.  
  157. At (1) the client is telling the server that it wants a connection.
  158. This is the SYN flag's only purpose.  The client is telling the
  159. server that the sequence number field is valid, and should be checked.
  160. The client will set the sequence number field in the TCP header to
  161. it's ISN (initial sequence number).  The server, upon receiving this
  162. segment (2) will respond with it's own ISN (therefore the SYN flag is
  163. on) and an ACKnowledgement of the clients first segment (which is the
  164. client's ISN+1).  The client then ACK's the server's ISN (3).  Now,
  165. data transfer may take place.
  166.  
  167.         --[ The ISN and Sequence Number Incrementation ]--
  168.  
  169.         It is important to understand how sequence numbers are
  170. initially choosen, and how they change with respect to time.  The
  171. initial sequence number when a host is bootstraped is initialized
  172. to 1. (TCP actually calls this variable 'tcp_iss' as it is the initial
  173. *send* sequence number.  The other sequence number variable,
  174. 'tcp_irs' is the initial *receive* sequence number and is learned
  175. during the 3-way connection establishment.  We are not going to worry
  176. about the distinction.)  This practice is wrong, and is acknowledged
  177. as so in a comment the tcp_init() function where it appears.  The ISN
  178. is incremented by 128,000 every second, which causes the 32-bit ISN
  179. counter to wrap every 9.32 hours if no connections occur.  However,
  180. each time a connect() is issued, the counter is incremented by
  181. 64,000.
  182.         One important reason behind this predictibility is to
  183. minimize the chance that data from an older stale incarnation
  184. (that is, from the same 4-tuple of the local and remote
  185. IP-addresses TCP ports) of the current connection could arrive
  186. and foul things up.  The concept of the 2MSL wait time applies
  187. here, but is beyond the scope of this paper.  If sequence
  188. numbers were choosen at random when a connection arrived, no
  189. guarantees could be made that the sequence numbers would be different
  190. from a previous incarnation.  If some data that was stuck in a
  191. routing loop somewhere finally freed itself and wandered into the new
  192. incarnation of it's old connection, it could really foul things up.
  193.  
  194.         --[ Ports ]--
  195.  
  196.         To grant simultaneous access to the TCP module, TCP provides
  197. a user interface called a port.  Ports are used by the kernel to
  198. identify network processes.  These are strictly transport layer
  199. entities (that is to say that IP could care less about them).
  200. Together with an IP address, a TCP port provides provides an endpoint
  201. for network communications.  In fact, at any given moment *all*
  202. Internet connections can be described by 4 numbers: the source IP
  203. address and source port and the destination IP address and destination
  204. port.  Servers are bound to 'well-known' ports so that they may be
  205. located on a standard port on different systems.  For example, the
  206. rlogin daemon sits on TCP port 513.
  207.  
  208.                 [SECTION II.  THE ATTACK]
  209.  
  210.         ...The devil finds work for idle hands....
  211.  
  212.         --[ Briefly... ]--
  213.  
  214.         IP-spoofing consists of several steps, which I will
  215. briefly outline here, then explain in detail.  First, the target host
  216. is choosen.  Next, a pattern of trust is discovered, along with a
  217. trusted host.  The trusted host is then disabled, and the target's TCP
  218. sequence numbers are sampled.  The trusted host is impersonated, the
  219. sequence numbers guessed, and a connection attempt is made to a
  220. service that only requires address-based authentication.  If
  221. successful, the attacker executes a simple command to leave a
  222. backdoor.
  223.  
  224.         --[ Needful Things ]--
  225.  
  226.         There are a couple of things one needs to wage this attack:
  227.  
  228.                 (1) brain, mind, or other thinking device
  229.                 (1) target host
  230.                 (1) trusted host
  231.                 (1) attacking host (with root access)
  232.                 (1) IP-spoofing software
  233.  
  234. Generally the attack is made from the root account on the attacking
  235. host against the root account on the target.  If the attacker is
  236. going to all this trouble, it would be stupid not to go for root.
  237. (Since root access is needed to wage the attack, this should not
  238. be an issue.)
  239.  
  240.         --[ IP-Spoofing is a 'Blind Attack' ]--
  241.  
  242.         One often overlooked, but critical factor in IP-spoofing
  243. is the fact that the attack is blind.  The attacker is going to be
  244. taking over the identity of a trusted host in order to subvert the
  245. security of the target host.  The trusted host is disabled using the
  246. method described below.  As far as the target knows, it is carrying on
  247. a conversation with a trusted pal.  In reality, the attacker is
  248. sitting off in some dark corner of the Internet, forging packets
  249. puportedly from this trusted host while it is locked up in a denial
  250. of service battle.  The IP datagrams sent with the forged IP-address
  251. reach the target fine (recall that IP is a connectionless-oriented
  252. protocol--  each datagram is sent without regard for the other end)
  253. but the datagrams the target sends back (destined for the trusted
  254. host) end up in the bit-bucket.  The attacker never sees them.  The
  255. intervening routers know where the datagrams are supposed to go.  They
  256. are supposed to go the trusted host.  As far as the network layer is
  257. concerned, this is where they originally came from, and this is where
  258. responses should go.  Of course once the datagrams are routed there,
  259. and the information is demultiplexed up the protocol stack, and
  260. reaches TCP, it is discarded (the trusted host's TCP cannot respond--
  261. see below).  So the attacker has to be smart and *know* what was sent,
  262. and *know* what reponse the server is looking for.  The attacker
  263. cannot see what the target host sends, but she can *predict* what it
  264. will send; that coupled with the knowledge of what it *will* send,
  265. allows the attacker to work around this blindness.
  266.  
  267.         --[ Patterns of Trust ]--
  268.  
  269.         After a target is choosen the attacker must determine the
  270. patterns of trust (for the sake of argument, we are going to assume
  271. the target host *does* in fact trust somebody.  If it didn't, the
  272. attack would end here).  Figuring out who a host trusts may or may
  273. not be easy.  A 'showmount -e' may show where filesystems are
  274. exported, and rpcinfo can give out valuable information as well.
  275. If enough background information is known about the host, it should
  276. not be too difficult.  If all else fails, trying neighboring IP
  277. addresses in a brute force effort may be a viable option.
  278.  
  279.         --[ Trusted Host Disabling Using the Flood of Sins ]--
  280.  
  281.         Once the trusted host is found, it must be disabled.  Since
  282. the attacker is going to impersonate it, she must make sure this host
  283. cannot receive any network traffic and foul things up.  There are
  284. many ways of doing this, the one I am going to discuss is TCP SYN
  285. flooding.
  286.         A TCP connection is initiated with a client issuing a
  287. request to a server with the SYN flag on in the TCP header.  Normally
  288. the server will issue a SYN/ACK back to the client identified by the
  289. 32-bit source address in the IP header.  The client will then send an
  290. ACK to the server (as we saw in figure 1 above) and data transfer
  291. can commence.  There is an upper limit of how many concurrent SYN
  292. requests TCP can process for a given socket, however.  This limit
  293. is called the backlog, and it is the length of the queue where
  294. incoming (as yet incomplete) connections are kept.  This queue limit
  295. applies to both the number of imcomplete connections (the 3-way
  296. handshake is not complete) and the number of completed connections
  297. that have not been pulled from the queue by the application by way of
  298. the accept() system call.  If this backlog limit is reached, TCP will
  299. silently discard all incoming SYN requests until the pending
  300. connections can be dealt with.  Therein lies the attack.
  301.         The attacking host sends several SYN requests to the TCP port
  302. she desires disabled.  The attacking host also must make sure that
  303. the source IP-address is spoofed to be that of another, currently
  304. unreachable host (the target TCP will be sending it's response to
  305. this address.  (IP may inform TCP that the host is unreachable,
  306. but TCP considers these errors to be transient and leaves the
  307. resolution of them up to IP (reroute the packets, etc) effectively
  308. ignoring them.)  The IP-address must be unreachable because the
  309. attacker does not want any host to recieve the SYN/ACKs that will be
  310. coming from the target TCP (this would result in a RST being sent to
  311. the target TCP, which would foil our attack).  The process is as
  312. follows:
  313.  
  314.                 fig(2)
  315.  
  316. 1       Z(x)    ---SYN--->      B
  317.  
  318.         Z(x)    ---SYN--->      B
  319.  
  320.         Z(x)    ---SYN--->      B
  321.  
  322.         Z(x)    ---SYN--->      B
  323.  
  324.         Z(x)    ---SYN--->      B
  325.  
  326.                 ...
  327.  
  328. 2       X    <---SYN/ACK---     B
  329.  
  330.         X    <---SYN/ACK---     B
  331.  
  332.                 ...
  333.  
  334. 3       X      <---RST---       B
  335.  
  336. At (1) the attacking host sends a multitude of SYN requests to the
  337. target (remember the target in this phase of the attack is the
  338. trusted host) to fill it's backlog queue with pending connections.
  339. (2) The target responds with SYN/ACKs to what it believes is the
  340. source of the incoming SYNs.  During this time all further requests
  341. to this TCP port will be ignored.
  342.         Different TCP implementations have different backlog sizes.
  343. BSD generally has a backlog of 5 (Linux has a backlog of 6).  There
  344. is also a 'grace' margin of 3/2.  That is, TCP will allow up to
  345. backlog*3/2+1 connections.  This will allow a socket one connection
  346. even if it calls listen with a backlog of 0.
  347.  
  348.         AuthNote: [For a much more in-depth treatment of TCP SYN
  349. flooding, see my definitive paper on the subject.  It covers the
  350. whole process in detail, in both theory, and practice.  There is
  351. robust working code, a statistical analysis, and a legnthy paper.
  352. Look for it in issue 49 of Phrack. -daemon9 6/96]
  353.  
  354.         --[ Sequence Number Sampling and Prediction ]--
  355.  
  356.         Now the attacker needs to get an idea of where in the 32-bit
  357. sequence number space the target's TCP is.  The attacker connects to
  358. a TCP port on the target (SMTP is a good choice) just prior to launching
  359. the attack and completes the three-way handshake.  The process is
  360. exactly the same as fig(1), except that the attacker will save the
  361. value of the ISN sent by the target host.  Often times, this process is
  362. repeated several times and the final ISN sent is stored.  The attacker
  363. needs to get an idea of what the RTT (round-trip time) from the target
  364. to her host is like.  (The process can be repeated several times, and an
  365. average of the RTT's is calculated.)  The RTT is necessary in being
  366. able to accuratly predict the next ISN.  The attacker has the baseline
  367. (the last ISN sent) and knows how the sequence numbers are incremented
  368. (128,000/second and 64,000 per connect) and now has a good idea of
  369. how long it will take an IP datagram to travel across the Internet to
  370. reach the target (approximately half the RTT, as most times the
  371. routes are symmetrical).  After the attacker has this information, she
  372. immediately proceeds to the next phase of the attack (if another TCP
  373. connection were to arrive on any port of the target before the
  374. attacker was able to continue the attack, the ISN predicted by the
  375. attacker would be off by 64,000 of what was predicted).
  376.         When the spoofed segment makes it's way to the target,
  377. several different things may happen depending on the accuracy of
  378. the attacker's prediction:
  379. - If the sequence number is EXACTly where the receiving TCP expects
  380. it to be, the incoming data will be placed on the next available
  381. position in the receive buffer.
  382. - If the sequence number is LESS than the expected value the data
  383. byte is considered a retransmission, and is discarded.
  384. - If the sequence number is GREATER than the expected value but
  385. still within the bounds of the receive window, the data byte is
  386. considered to be a future byte, and is held by TCP, pending the
  387. arrival of the other missing bytes.  If a segment arrives with a
  388. sequence number GREATER than the expected value and NOT within the
  389. bounds of the receive window the segment is dropped, and TCP will
  390. send a segment back with the *expected* sequence number.
  391.  
  392.         --[ Subversion... ]--
  393.  
  394.         Here is where the main thrust of the attack begins:
  395.  
  396.                 fig(3)
  397.  
  398. 1       Z(b)    ---SYN--->      A
  399.  
  400. 2       B     <---SYN/ACK---    A
  401.  
  402. 3       Z(b)    ---ACK--->      A
  403.  
  404. 4       Z(b)    ---PSH--->      A
  405.  
  406.                 [...]
  407.  
  408. The attacking host spoofs her IP address to be that of the trusted
  409. host (which should still be in the death-throes of the D.O.S. attack)
  410. and sends it's connection request to port 513 on the target (1).  At
  411. (2), the target responds to the spoofed connection request with a
  412. SYN/ACK, which will make it's way to the trusted host (which, if it
  413. *could* process the incoming TCP segment, it would consider it an
  414. error, and immediately send a RST to the target).  If everything goes
  415. according to plan, the SYN/ACK will be dropped by the gagged trusted
  416. host.  After (1), the attacker must back off for a bit to give the
  417. target ample time to send the SYN/ACK (the attacker cannot see this
  418. segment).  Then, at (3) the attacker sends an ACK to the target with
  419. the predicted sequence number (plus one, because we're ACKing it).
  420. If the attacker is correct in her prediction, the target will accept
  421. the ACK.  The target is compromised and data transfer can
  422. commence (4).
  423.         Generally, after compromise, the attacker will insert a
  424. backdoor into the system that will allow a simpler way of intrusion.
  425. (Often a `cat + + >> ~/.rhosts` is done.  This is a good idea for
  426. several reasons: it is quick, allows for simple re-entry, and is not
  427. interactive.  Remember the attacker cannot see any traffic coming from
  428. the target, so any reponses are sent off into oblivion.)
  429.  
  430.         --[ Why it Works ]--
  431.  
  432.         IP-Spoofing works because trusted services only rely on
  433. network address based authentication.  Since IP is easily duped,
  434. address forgery is not difficult.  The hardest part of the attck is
  435. in the sequence number prediction, because that is where the guesswork
  436. comes into play.  Reduce unknowns and guesswork to a minimum, and
  437. the attack has a better chance of suceeding.  Even a machine that
  438. wraps all it's incoming TCP bound connections with Wietse Venema's TCP
  439. wrappers, is still vulnerable to the attack.  TCP wrappers rely on a
  440. hostname or an IP address for authentication...
  441.  
  442.                 [SECTION III. PREVENTITIVE MEASURES]
  443.  
  444.         ...A stich in time, saves nine...
  445.  
  446.         --[ Be Un-trusting and Un-trustworthy ]--
  447.  
  448.         One easy solution to prevent this attack is not to rely
  449. on address-based authentication.  Disable all the r* commands,
  450. remove all .rhosts files and empty out the /etc/hosts.equiv file.
  451. This will force all users to use other means of remote access
  452. (telnet, ssh, skey, etc).
  453.  
  454.         --[ Packet Filtering ]--
  455.  
  456.         If your site has a direct connect to the Internet, you
  457. can use your router to help you out.  First make sure only hosts
  458. on your internal LAN can particpate in trust-relationships (no
  459. internal host should trust a host outside the LAN).  Then simply
  460. filter out *all* traffic from the outside (the Internet) that
  461. puports to come from the inside (the LAN).
  462.  
  463.         --[ Cryptographic Methods ]--
  464.  
  465.         An obvious method to deter IP-spoofing is to require
  466. all network traffic to be encrypted and/or authenticated.  While
  467. several solutions exist, it will be a while before such measures are
  468. deployed as defacto standards.
  469.  
  470.         --[ Initial Sequence Number Randomizing ]--
  471.  
  472.         Since the sequence numbers are not choosen randomly (or
  473. incremented randomly) this attack works.  Bellovin describes a
  474. fix for TCP that involves partitioning the sequence number space.
  475. Each connection would have it's own seperate sequence number space.
  476. The sequence numbers would still be incremented as before, however,
  477. there would be no obvious or implied relationship between the
  478. numbering in these spaces.  Suggested is the following formula:
  479.  
  480.         ISN=M+F(localhost,localport,remotehost,remoteport)
  481.  
  482. Where M is the 4 microsecond timer and F is a cryptographic hash.
  483. F must not be computable from the outside or the attacker could
  484. still guess sequence numbers.  Bellovin suggests F be a hash of
  485. the connection-id and a secret vector (a random number, or a host
  486. related secret combined with the machine's boot time).
  487.  
  488.                 [SECTION IV.  SOURCES]
  489.  
  490.         -Books:         TCP/IP Illustrated vols. I, II & III
  491.         -RFCs:          793, 1825, 1948
  492.         -People:        Richard W. Stevens, and the users of the
  493.                         Information Nexus for proofreading
  494.         -Sourcecode:    rbone, mendax, SYNflood
  495.  
  496. This paper made possible by a grant from the Guild Corporation.
  497.